package org.abettor.angleworm.jetty;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class TestServlet extends HttpServlet
{
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		System.out.println(req.getRequestURL());

		String encoding = req.getCharacterEncoding();
		if(encoding == null || encoding.isEmpty())
		{
			encoding = "iso-8859-1";
		}

		InputStream is = req.getInputStream();
		int len = is.available();
		byte[] b = new byte[len];
		is.read(b);
		String content = new String(b, encoding);
		System.out.println(content);

		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html");
		PrintWriter pw = resp.getWriter();
		pw.println("<html>");
		pw.println("<head>");
		pw.println("<title>Jetty Servlet Demo</title>");
		pw.println("</head>");
		pw.println("<body>");
		pw.println("<h1>内容1</h1>");
		pw.println("<h1>内容2</h1>");
		pw.println("</body>");
		pw.println("</html>");
		pw.flush();
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		doPost(req, resp);
	}
}
